package com.yidu.best.bond.securitiesInventory.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yidu.best.bond.bondMarket.dao.BondMarketDao;
import com.yidu.best.bond.bondMarket.domain.BondMarket;
import com.yidu.best.bond.bondTrading.dao.BondTradeDao;
import com.yidu.best.bond.bondTrading.domain.BondTrade;
import com.yidu.best.bond.cashArap.dao.CashArapDao;
import com.yidu.best.bond.cashArap.domain.CashArap;
import com.yidu.best.bond.casharapInventory.dao.CasharapInventoryDao;
import com.yidu.best.bond.casharapInventory.domain.CasharapInventory;
import com.yidu.best.bond.securitiesArap.dao.SecuritiesArapDao;
import com.yidu.best.bond.securitiesArap.domain.SecuritiesArap;
import com.yidu.best.bond.securitiesInventory.dao.SecuritiesInventoryDao;
import com.yidu.best.bond.securitiesInventory.domain.SecuritiesInventory;
import com.yidu.best.bond.securitiesInventory.domain.SecuritiesInventoryVo;
import com.yidu.best.bond.securitiesInventory.service.SecuritiesInventoryService;
import com.yidu.best.util.IDUtil;

@Service
public class SecuritiesInventoryServiceImpl implements SecuritiesInventoryService {
	@Autowired
	private SecuritiesInventoryDao securitiesInventoryDao;
	@Autowired
	private CashArapDao cashArapDao;
	@Autowired
	private BondTradeDao bondTradeDao;
	@Autowired
	private CasharapInventoryDao casharapInventoryDao;
	@Autowired
	private BondMarketDao bondMarketDao;
	@Autowired
	private SecuritiesArapDao securitiesArapDao;

	@Override
	public Map<String, Object> findPageQuery(SecuritiesInventoryVo securitiesInventoryVo) {
		// 调用dao层方法查询数量
		int count = securitiesInventoryDao.countSecuritiesInventory(securitiesInventoryVo);
		// 计算页码
		int page = (securitiesInventoryVo.getPage() - 1) * securitiesInventoryVo.getLimit();
		securitiesInventoryVo.setPage(page);
		// 调用dao层方法分页查询
		List<SecuritiesInventory> capitalTransferList = securitiesInventoryDao.findPageQuery(securitiesInventoryVo);
		Map<String, Object> map = new HashMap<String, Object>();
		// 将数据存入map中
		map.put("code", 0);
		map.put("count", count);
		map.put("data", capitalTransferList);
		return map;
	}

	@Override
	public boolean updateSecuritiesInventoryStatus(String status, String securitiesInventoryId) {
		int rs = 0, cashArapRs = 0, casharapInventoryRs = 0;
		// 定义数组接受前台返回的字符串数组 将,去掉
		String[] securitiesInventoryIdS = securitiesInventoryId.split(",");
		String[] statusS = status.split(",");
		// 将接收的数组遍历
		for (int i = 0; i < securitiesInventoryIdS.length; i++) {
			// 将tradeStatus转为Integer类型
			Integer status1 = Integer.valueOf(statusS[i]);
			SecuritiesInventory securitiesInventory = securitiesInventoryDao
					.findSecuritiesInventoryIdBysecuritiesInventoryId(securitiesInventoryIdS[i]);
			// 判断状态为0的为未结算
			if (status1 == 0) {
				rs = securitiesInventoryDao.updateSecuritiesInventoryStatus(1, securitiesInventoryIdS[i]);
				cashArapRs = doainterestOrupdateCashArap(securitiesInventory);
				casharapInventoryRs = doainterestOrupdateCasharapInventory(securitiesInventory);
			} else if (status1 == 1) {// 判断状态为1的为已未结算
				rs = securitiesInventoryDao.updateSecuritiesInventoryStatus(0, securitiesInventoryIdS[i]);
				casharapInventoryRs = doainterestOrupdateCasharapInventory(securitiesInventory);
			}
		}
		if (rs >= 1 && cashArapRs >= 1 && casharapInventoryRs >= 1) {
			return true;
		}
		return false;
	}

	/**
	 * 添加或修改现金应收应付
	 * 
	 * @param securitiesInventory
	 * @return
	 */
	public int doainterestOrupdateCashArap(SecuritiesInventory securitiesInventory) {
		// 根据基金ID和证券ID查询现金应收应付
		CashArap cashArap = cashArapDao.findCashArapByFundIdandSecuritiesId(securitiesInventory.getFundId(),
				securitiesInventory.getSecuritiesId(), 4);
		int rs = 0;
		// 根据基金ID和债券Id查询债券交易数据
		BondTrade bondTrade = bondTradeDao.findBondTradeByFundIdandbondId(securitiesInventory.getFundId(),
				securitiesInventory.getSecuritiesId());
		// 获得总金额
		BigDecimal turnover = securitiesInventory.getTurnover();
		// 计算一年的利息
		BigDecimal interest = turnover.multiply(bondTrade.getSecurityInterest());
		// 计算一天的利息
		BigDecimal dayInterest = interest.divide(BigDecimal.valueOf(365), 2);
		if (cashArap == null) {// 如果现金应收应付未空就添加一条记录
			CashArap cashArap1 = new CashArap(IDUtil.getUUID(), IDUtil.getNo("XJYSYF"), securitiesInventory.getFundId(),
					securitiesInventory.getFundNo(), securitiesInventory.getFundName(),
					securitiesInventory.getAccountId(), securitiesInventory.getAccountNo(),
					securitiesInventory.getAccountName(),securitiesInventory.getSecuritiesId(),securitiesInventory.getSecuritiesNo(),securitiesInventory.getSecuritiesName(), dayInterest, new Date(), 1, 4);
			// 调用Dao层添加现金应收应付
			rs = cashArapDao.insertCashArap(cashArap1);
		} else {// 如果现金应收应付存在就修改一条记录
			CashArap cashArap1 = new CashArap();
			// 修改利息
			if (cashArap.getArapFlag() == 1) {
				cashArap1.setArapAmount(cashArap.getArapAmount().add(dayInterest));
			} else if (cashArap.getArapFlag() == 0) {
				cashArap1.setArapAmount(cashArap.getArapAmount().subtract(dayInterest));
			}
			cashArap1.setCashARAPId(cashArap.getCashARAPId());
			// 修改时间
			cashArap1.setArapDate(new Date());
			rs = cashArapDao.updateCashArap(cashArap1);
		}
		return rs;
	}

	/**
	 * 添加或修改现金应收应付库存
	 * 
	 * @param securitiesInventory
	 * @return
	 */
	public int doainterestOrupdateCasharapInventory(SecuritiesInventory securitiesInventory) {
		// 根据基金ID和证券ID查询现金应收应付
		CasharapInventory casharapInventory = casharapInventoryDao.findCasharapInventoryByFundIdandSecuritiesId(
				securitiesInventory.getFundId(), securitiesInventory.getAccountId(), 2);
		int rs = 0;
		// 根据基金ID和债券Id查询债券交易数据
		BondTrade bondTrade = bondTradeDao.findBondTradeByFundIdandbondId(securitiesInventory.getFundId(),
				securitiesInventory.getSecuritiesId());
		// 获得总金额
		BigDecimal turnover = securitiesInventory.getTurnover();
		// 计算一年的利息
		BigDecimal interest = turnover.multiply(bondTrade.getSecurityInterest());
		// 计算一天的利息
		BigDecimal dayInterest = interest.divide(BigDecimal.valueOf(365), 2);
		if (casharapInventory == null) {// 如果现金应收应付未空就添加一条记录
			CasharapInventory casharapInventory1 = new CasharapInventory(IDUtil.getUUID(), IDUtil.getNo("XJYSYFKC"),
					securitiesInventory.getFundId(), securitiesInventory.getFundNo(), securitiesInventory.getFundName(),
					securitiesInventory.getAccountId(), securitiesInventory.getAccountNo(),
					securitiesInventory.getAccountName(), dayInterest, new Date(), 5, 1, null);
			// 调用Dao层添加现金应收应付
			rs = casharapInventoryDao.insertCasharapInventory(casharapInventory1);
		} else {// 如果现金应收应付存在就修改一条记录
			CasharapInventory casharapInventory1 = new CasharapInventory();
			// 修改利息
			if (casharapInventory.getFlag() == 1) {
				casharapInventory1.setBalance(casharapInventory.getBalance().add(dayInterest));
			} else if (casharapInventory.getFlag() == 0) {
				casharapInventory1.setBalance(casharapInventory.getBalance().subtract(dayInterest));
			}
			casharapInventory1.setCachARAPInventoryId(casharapInventory.getCachARAPInventoryId());
			// 修改时间
			casharapInventory1.setBusinessDate(new Date());
			rs = casharapInventoryDao.updateCasharapInventory(casharapInventory1);
		}
		return rs;
	}

	@Override
	public boolean updateSecuritiesInventory(String securitiesInventoryId) {
		int rs = 0,securitiesArapRs=0;
		String[] securitiesInventoryIdS = securitiesInventoryId.split(",");
		for (int i = 0; i < securitiesInventoryIdS.length; i++) {
			//根据证券库存Id查询证券库存
			SecuritiesInventory securitiesInventory = securitiesInventoryDao.findSecuritiesInventoryIdBysecuritiesInventoryId(securitiesInventoryIdS[i]);
			//根据行情数据Id查询行情数据
			BondMarket bondMarket = bondMarketDao.findBondMarketById(securitiesInventory.getSecuritiesId());
			
			SecuritiesInventory securitiesInventorys = new SecuritiesInventory();
			//
			securitiesInventorys.setSecuritiesInventoryId(securitiesInventoryIdS[i]);
			//
			securitiesInventorys.setNav(bondMarket.getOpeningPrice());
			//计算总金额
			BigDecimal turnover = bondMarket.getOpeningPrice().multiply(BigDecimal.valueOf(securitiesInventory.getShare()));
			securitiesInventorys.setTurnover(turnover);
			//调用dao层方法
			rs = securitiesInventoryDao.doSecuritiesInventoryappraisement(securitiesInventorys);
			//
			securitiesArapRs = insertSecuritiesArap(securitiesInventory);
		}
		if(rs>=1&&securitiesArapRs>=1) {
			return true;
		}
		return false;
	}
	
	
	public int insertSecuritiesArap(SecuritiesInventory securitiesInventory) {
		SecuritiesArap SecuritiesArapRs = securitiesArapDao.findSecuritiesArapByFundIdandSecuritiesId(securitiesInventory.getFundId(), securitiesInventory.getSecuritiesId());
		if(SecuritiesArapRs==null) {//判断是否为空
			//为空就添加
			SecuritiesArap securitiesArap = new SecuritiesArap(
					IDUtil.getUUID(),IDUtil.getNo("ZQYSYF"),securitiesInventory.getFundId(),securitiesInventory.getFundNo(),securitiesInventory.getFundName(),
					securitiesInventory.getAccountId(),securitiesInventory.getAccountNo(),securitiesInventory.getAccountName(),securitiesInventory.getSecuritiesId(),
					securitiesInventory.getSecuritiesNo(),securitiesInventory.getSecuritiesName(),securitiesInventory.getTurnover(),new Date(),1,2
				);
			return securitiesArapDao.insertSecuritiesArap(securitiesArap);
		}else {//不为空就修改
			SecuritiesArap securitiesArap = new SecuritiesArap();
			securitiesArap.setSecuritiesARAPId(SecuritiesArapRs.getSecuritiesARAPId());
			securitiesArap.setArapAmount(SecuritiesArapRs.getArapAmount().add(securitiesInventory.getTurnover()));
			return  securitiesArapDao.updateSecuritiesArap(securitiesArap);
		}
		
	}

}
